﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;

namespace WindowsFormProject.Depo
{
    public class UrunSubeIslemler
    {
        public class SubeUrunTut
        {

            public int SubeID { get; set; }
            public string SubeAdi { get; set; }
            public string UrunId { get; set; }
            public string UrunADİ { get; set; }
            public string KatAdi { get; set; }
            public decimal SubeStokAdet { get; set; }
            public double SubeSatisFiyati { get; set; }
            public decimal SubeKDV { get; set; }
            public decimal SubeMaxIskontoOrani { get; set; }
            public decimal SubeIskontoOrani { get; set; }
            public bool SubeSatistaMı { get; set; }
            public string SubeSatisCikisTarihi { get; set; }
            public string SubeSatisCikisSebebi { get; set; }
            public override string ToString()
            {
                return SubeID + "" + SubeAdi + "" + UrunId + " " + UrunADİ + "" + KatAdi +""+SubeStokAdet + SubeSatisFiyati + "" + SubeKDV + "" + SubeMaxIskontoOrani + "" + SubeIskontoOrani + "" + SubeSatisCikisSebebi;

            }
        }
        public class SubeUrundetayIslemler : IEnumerable
        {
            DataBaseProvider MyDataBase = new DataBaseProvider();
            
            List<SubeUrunTut> SubeUrundetaylarilist = new List<SubeUrunTut>();
                        
            public DataTable SubeUrunler(int SubeID, string UrunId)
            {

                DataTable ReturnValue;
                MyDataBase.AddInParameter("@SubeID", SqlDbType.Int, SubeID);
                MyDataBase.AddInParameter("@UrunId", SqlDbType.NVarChar, UrunId);
                ReturnValue = MyDataBase.ExecuteDataTable("Select SU.SubeID,SU.UrunId,SU.SubeStokAdet,SU.SubeSatisFiyati,SU.SubeKDV,SU.SubeMaxIskontoOrani,SU.SubeIskontoOrani,SU.SubeSatistaMı,SU.SubeSatisCikisTarihi,SU.SubeSatisCikisSebebi,K.KatagoriAdi, MU.UrunADi,S.SubeAdi from SubeUrunler AS SU Left Outer Join MErkezUrunler AS MU ON MU.UrunId=SU.UrunId Left Outer JOIN Kategoriler AS K ON K.KatID= MU.KatID  Left Outer Join Subeler AS S ON SU.SubeID= S.SubeID where SU.UrunId=@UrunId and S.SubeID=@SubeID", CommandType.Text);
                MyDataBase.ClearParameters();
                return ReturnValue;
            }
            public List<SubeUrunTut> SubeUrunIdyeGore(int SubeID,string recID)
            {
                List<SubeUrunTut> SecilenUrun = new List<SubeUrunTut>();

                foreach (SubeUrunTut item in SubeUrundetaylarilist)
                {
                    if (item.UrunId == recID && item.SubeID == SubeID)
                    {
                        SecilenUrun.Add(item);
                    }
                }
                return SecilenUrun;
            }
            public void SubeUrunEkle(SubeUrunTut U)
            {
                SubeUrundetaylarilist.Add(U);
            }
            public void SubeUrunTutClear(SubeUrunTut U)
            {
                SubeUrundetaylarilist.Clear();

            }
            public int Get_RecordSubeUrunCount(int SubeID, string UrunId)
            {
                int ReturnValue = 0;
                MyDataBase.AddInParameter("@SubeID", SqlDbType.Int, SubeID);
                MyDataBase.AddInParameter("@UrunId", SqlDbType.NVarChar, UrunId);
                ReturnValue = Convert.ToInt32(MyDataBase.ExecuteScalar("Select UrunId from SubeUrunler where UrunId = @UrunId and SubeID=@SubeID", CommandType.Text));
                MyDataBase.ClearParameters();
                return ReturnValue;
            }

            public int SubeUrundelete(int SubeID, string UrunId)
            {
                int ReturnValue = 0;
           
                MyDataBase.AddInParameter("@SubeID", SqlDbType.Int, SubeID);
                MyDataBase.AddInParameter("@UrunId", SqlDbType.NVarChar, UrunId);
                ReturnValue = Convert.ToInt32(MyDataBase.ExecuteNonQuery("Delete  SubeUrunler where UrunId = @UrunId and SubeID=@SubeID ", CommandType.Text));
                MyDataBase.ClearParameters();
                return ReturnValue;
            }
            public int SubeUrunVeriTabaninaKayit(SubeUrunTut U)
            {
                int ReturnValue = 0;
                int ToplamEtkilenenKayitSayisi = 0;
                foreach (SubeUrunTut item in SubeUrundetaylarilist)
                {
                    try
                    {
                        MyDataBase.AddInParameter("@SubeID", SqlDbType.Int,U.SubeID);
                        MyDataBase.AddInParameter("@UrunId", SqlDbType.NVarChar,U.UrunId);
                        MyDataBase.AddInParameter("@SubeStokAdet", SqlDbType.Decimal, U.SubeStokAdet);
                        MyDataBase.AddInParameter("@SubeSatisFiyati", SqlDbType.Money, U.SubeSatisFiyati);
                        MyDataBase.AddInParameter("@SubeKDV", SqlDbType.Decimal, U.SubeKDV);
                        MyDataBase.AddInParameter("@SubeMaxIskontoOrani", SqlDbType.Decimal, U.SubeMaxIskontoOrani);
                        MyDataBase.AddInParameter("@SubeIskontoOrani", SqlDbType.Decimal, U.SubeIskontoOrani);
                        MyDataBase.AddInParameter("@SubeSatistaMı", SqlDbType.Bit, U.SubeSatistaMı);
                        MyDataBase.AddInParameter("@SubeSatisCikisTarihi", SqlDbType.DateTime, U.SubeSatisCikisTarihi);
                        MyDataBase.AddInParameter("@SubeSatisCikisSebebi", SqlDbType.NVarChar, U.SubeSatisCikisSebebi);
                        MyDataBase.ExecuteNonQuery("Insert Into SubeUrunler (SubeID,UrunId,SubeStokAdet,SubeSatisFiyati,SubeKDV,SubeMaxIskontoOrani,SubeIskontoOrani,SubeSatistaMı,SubeSatisCikisTarihi,SubeSatisCikisSebebi) values (@SubeID,@UrunId,@SubeStokAdet,@SubeSatisFiyati,@SubeKDV,@SubeMaxIskontoOrani,@SubeIskontoOrani,@SubeSatistaMı,@SubeSatisCikisTarihi,@SubeSatisCikisSebebi)", CommandType.Text);
                    }
                    catch
                    {
                    }
                    MyDataBase.ClearParameters();
                    return ReturnValue;
                }
              
                return ToplamEtkilenenKayitSayisi;
            }
            public int SubeUrunKayitGuncelle(SubeUrunTut U)
            {
                int ReturnValue = 0;
                foreach (SubeUrunTut item in SubeUrundetaylarilist)
                {
                    try
                    {
                        MyDataBase.AddInParameter("@SubeID", SqlDbType.Int, U.SubeID);
                        MyDataBase.AddInParameter("@UrunId", SqlDbType.NVarChar, U.UrunId);
                        MyDataBase.AddInParameter("@SubeStokAdet", SqlDbType.Decimal, U.SubeStokAdet);
                        MyDataBase.AddInParameter("@SubeSatisFiyati", SqlDbType.Money, U.SubeSatisFiyati);
                        MyDataBase.AddInParameter("@SubeKDV", SqlDbType.Decimal, U.SubeKDV);
                        MyDataBase.AddInParameter("@SubeMaxIskontoOrani", SqlDbType.Decimal, U.SubeMaxIskontoOrani);
                        MyDataBase.AddInParameter("@SubeIskontoOrani", SqlDbType.Decimal, U.SubeIskontoOrani);
                        MyDataBase.AddInParameter("@SubeSatistaMı", SqlDbType.Bit, U.SubeSatistaMı);
                        MyDataBase.AddInParameter("@SubeSatisCikisTarihi", SqlDbType.DateTime, U.SubeSatisCikisTarihi);
                        MyDataBase.AddInParameter("@SubeSatisCikisSebebi", SqlDbType.NVarChar, U.SubeSatisCikisSebebi);
                        MyDataBase.ExecuteNonQuery("Update SubeUrunler set SubeID =@SubeID,UrunId=@UrunId,SubeStokAdet=@SubeStokAdet,SubeSatisFiyati=@SubeSatisFiyati,SubeKDV=@SubeKDV,SubeMaxIskontoOrani=@SubeMaxIskontoOrani,SubeIskontoOrani=@SubeIskontoOrani,SubeSatistaMı=@SubeSatistaMı,SubeSatisCikisTarihi=@SubeSatisCikisTarihi,SubeSatisCikisSebebi=@SubeSatisCikisSebebi ", CommandType.Text);
                    }
                    catch
                    {

                    }

                }
                MyDataBase.ClearParameters();
                return ReturnValue;
            }

            public IEnumerator GetEnumerator()
            {
                return new Gezdirici(SubeUrundetaylarilist);

            }
        }
        public class Gezdirici : IEnumerator
        {
            List<SubeUrunTut> GelenListe;
            int ToplamEleman;
            int GecerliElemanNo;
            public Gezdirici(List<SubeUrunTut> Liste)
            {
                GelenListe = Liste;
                ToplamEleman = Liste.Count;
                GecerliElemanNo = -1;
            }
            public object Current
            {
                get
                {
                    return GelenListe[GecerliElemanNo];
                }
            }

            public bool MoveNext()
            {

                GecerliElemanNo++;
                if (GecerliElemanNo < ToplamEleman)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }

            public void Reset()
            {

                GecerliElemanNo = -1;
            }

        }
    }
}
